package test.com.sms.selenium.singlepoint.util;

import org.openqa.selenium.By;

import test.com.sms.selenium.framework.SeleniumPageUtil;
import test.com.sms.selenium.util.SeleniumUtil;

public class SpContractsAssetSearchPageUtil extends SeleniumPageUtil{
	public final static By CAS_SPAN_HEAD_TITLE = By.id("ctl44_hdrSaveClear_lblHeader");
	public final static By CAS_LINK_SEARCH = By.xpath("//*[@id='ddcf634a-a5bf-4f11-9467-6e6d8e050e40']/a");
	public final static By CAS_LINK_CLEAR = By.xpath("//*[@id='dc8aa588-ce68-459b-a3a0-a7f5183bd3da']/a");
	public final static By CAS_TD_CUSTOMER = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[1]/td[1]");
	public final static By CAS_INPUT_CUSTOMER = By.id("ctl44_txtCustomer");
	public final static By CAS_TD_CONTRACTID = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[1]/td[3]");
	public final static By CAS_INPUT_CONTRACTID = By.id("ctl44_txtContractID");
	public final static By CAS_LINK_GO = By.id("ctl44_btnGo");
	public final static By CAS_TD_WARE = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[2]/td[1]");
	public final static By CAS_INPUT_WARE = By.id("ctl44_txtWhseID");
	public final static By CAS_TD_ADD = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[2]/td[3]");
	public final static By CAS_INPUT_ADD = By.id("ctl44_txtAddress");
	public final static By CAS_TD_SER_NUM = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[3]/td[1]");
	public final static By CAS_INPUT_SER_NUM = By.id("ctl44_txtSerialNumber");
	public final static By CAS_TD_ASSET_NAME = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[3]/td[3]");
	public final static By CAS_INPUT_ASSET_NAME = By.id("ctl44_txtAssetName");
	public final static By CAS_TD_MFG_NAME = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[4]/td[1]");
	public final static By CAS_INPUT_MFG_NAME = By.id("ctl44_txtMfgName");
	public final static By CAS_TD_MODEL_CODE = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[4]/td[3]");
	public final static By CAS_INPUT_MODEL_CODE = By.id("ctl44_txtModelCode");
	public final static By CAS_UI_MODEL_CODE_LIST = By.id("aceModelCode_Behavior_completionListElem");
	public final static By CAS_TD_DESC = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[5]/td[1]");
	public final static By CAS_INPUT_DESC = By.id("ctl44_txtDescription");
	public final static By CAS_IMG_REFRESH = By.id("ctl44_AssetContractReport_Header_imgRefresh");
	public final static By CAS_SPAN_RESULT = By.id("ctl44_AssetContractReport_ReportOutput");
	
	// elements of the 'mfg name' list entering something into
	public final static By CAS_LIST_MFG_NAME = By.id("ctl44_aceMfgName_completionListElem");
	
	// elements of search results
	public final static By CAS_IMG_FILTER_1 = By.id("ctl44_AssetContractReport_filter_0");
	public final static By CAS_IMG_FILTER_2 = By.id("ctl44_AssetContractReport_filter_1");
	public final static By CAS_IMG_FILTER_3 = By.id("ctl44_AssetContractReport_filter_2");
	public final static By CAS_IMG_FILTER_4 = By.id("ctl44_AssetContractReport_filter_3");
	public final static By CAS_IMG_FILTER_5 = By.id("ctl44_AssetContractReport_filter_4");
	public final static By CAS_IMG_FILTER_6 = By.id("ctl44_AssetContractReport_filter_5");
	public final static By CAS_IMG_FILTER_7 = By.id("ctl44_AssetContractReport_filter_6");
	public final static By CAS_IMG_FILTER_8 = By.id("ctl44_AssetContractReport_filter_7");
	public final static By CAS_IMG_FILTER_9 = By.id("ctl44_AssetContractReport_filter_8");
	public final static By CAS_IMG_FILTER_10 = By.id("ctl44_AssetContractReport_filter_9");
	public final static By CAS_IMG_FILTER_11 = By.id("ctl44_AssetContractReport_filter_10");
	
	// elements of filter page
	public final static By CAS_INPUT_FILTER_CONTENT = By.id("ctl44_AssetContractReport_txtFilterInput");
	public final static By CAS_BUTTON_ENTER = By.id("ctl44_AssetContractReport_btnFilterEnter");
	public final static By CAS_BUTTON_CANCEL = By.id("ctl44_AssetContractReport_btnFilterCancel");
	public final static By CAS_BUTTON_CLEAR = By.id("ctl44_AssetContractReport_btnFilterClear");
	
	
	public static void enterContractsAssetSearchPage(){
		SpHomePageUtil.enter();
		SpHomePageUtil.enterContractsAssetSearch();
		waitForPageToLoad();
	}
	public static void enterAsset(){
		SpHomePageUtil.enter();
		SpHomePageUtil.changeCustomer("J WALTER INC.");
		SpHomePageUtil.enterContractsAssetSearch();
		waitForPageToLoad();
	}
	public static void waitForPageToLoad() {
		SeleniumUtil.selectDefaultContent();
		SeleniumUtil.selectFrame(SpHomePageUtil.HOME_FRAME_CONTENT_NAME);
		SeleniumUtil.waitOnElements(new By[]{
				CAS_INPUT_CUSTOMER,CAS_INPUT_MODEL_CODE,CAS_INPUT_DESC
		});
	}
	
	public static void waitForFilterPageToLoad() {
		SeleniumUtil.waitOnElements(new By[]{
				CAS_BUTTON_ENTER,CAS_BUTTON_CANCEL
		});
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CAS_BUTTON_ENTER));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CAS_BUTTON_CANCEL));
	}
	
	public static void waitForSearchResultsToLoad() {
		SeleniumUtil.waitOnElements(new By[]{
				CAS_IMG_FILTER_1, CAS_IMG_FILTER_2, CAS_IMG_FILTER_3, CAS_IMG_FILTER_4,
				CAS_IMG_FILTER_5, CAS_IMG_FILTER_6, CAS_IMG_FILTER_7, CAS_IMG_FILTER_8,
				 CAS_IMG_FILTER_9, CAS_IMG_FILTER_10, CAS_IMG_FILTER_11
		});
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CAS_IMG_FILTER_1));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CAS_IMG_FILTER_2));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CAS_IMG_FILTER_3));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CAS_IMG_FILTER_4));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CAS_IMG_FILTER_5));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CAS_IMG_FILTER_6));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CAS_IMG_FILTER_7));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CAS_IMG_FILTER_8));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CAS_IMG_FILTER_9));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CAS_IMG_FILTER_10));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CAS_IMG_FILTER_11));
	}
	
	public static void waitForResultsRefresh() {
		boolean flag = !SeleniumUtil.findElements(CAS_IMG_REFRESH).get(0).getAttribute("style").toLowerCase().contains("display: none");
		long before = System.currentTimeMillis();
		while(flag){
			if((System.currentTimeMillis() - before) > 30 * 1000){
				SeleniumUtil.fail("more than 30 seconds no response!");
				return;
			}else{
				flag = !SeleniumUtil.findElements(CAS_IMG_REFRESH).get(0).getAttribute("style").toLowerCase().contains("display: none");
			}
		}
	}
}
